<template>
  <div class="add">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="100px"
             class="yuyi-ruleForm">
      <el-form-item label="类别" prop="classifyName">
        <el-input size="small"
                  :disabled="type === 'see'"
                  v-model="ruleForm.classifyName"></el-input>
      </el-form-item>
      <el-form-item label="期限" prop="retentionType">
        <el-select size="small"
                   :disabled="type === 'see'"
                   v-model="ruleForm.retentionType"
                   class="width-all">
          <el-option v-for="(item, index) in dicLIst"
                     :key="index"
                     :value="item.dictValue"
                     :label="item.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上级类别">
        <tree-select-form :treeData="treeData"
                          :treeProps="treeProps"
                          v-model="ruleForm.pid">
        </tree-select-form>
      </el-form-item>
      <el-form-item label="排序" prop="orderNo">
        <avue-input-number :disabled="type === 'see'"
                           class="width-all"
                           size="small"
                           :precision="0"
                           v-model="ruleForm.orderNo"
                           :min-rows="1"
                           :max-rows="99999"></avue-input-number>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input type="textarea"
                  :autosize="autosize"
                  show-word-limit
                  maxlength="200"
                  :disabled="type === 'see'"
                  v-model="ruleForm.remark"></el-input>
      </el-form-item>

      <el-form-item v-if="type !== 'see'">
        <el-button type="primary"
                   size="small"
                   :loading="saveLoading"
                   @click="submitForm('ruleForm')">
          {{ type === 'add' ? '保存' : '确定修改'}}
        </el-button>
        <el-button size="small"
                   :loading="saveLoading"
                   @click="handleCancel">取消
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { addTermList, editTermList } from '../../../../../api/storagePeriod'
import TreeSelectForm from '../../../../../components/treeSelect/treeSelectForm'

export default {
  name: 'addEditSeePeriod',
  components: { TreeSelectForm },
  props: {
    type: {
      request: true,
      type: String
    },
    pid: {
      default: 0
    },
    idCategory: {
      default: ''
    },
    dicLIst: {
      type: Array,
      default: () => ([])
    },
    treeData: {
      type: Array,
      default: () => ([])
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      treeProps: {
        value: 'id',
        label: 'classifyName',
        children: 'children'
      },
      saveLoading: false,
      autosize: {
        minRows: 4,
        maxRows: 6
      },
      ruleForm: {
        classifyName: '', // 类别
        retentionType: '', // 期限
        pid: '', // 上级类别 - 父级
        orderNo: '', // 排序
        remark: '' // 描述
      },
      rules: {
        classifyName: [
          {
            required: true,
            message: '请输入类别',
            trigger: 'blur'
          }
        ],
        retentionType: [
          {
            required: true,
            message: '请选择期限',
            trigger: 'blur'
          }
        ],
        orderNo: [{
          required: true,
          message: '请输入排序号',
          trigger: 'blur'
        }]
      }
    }
  },
  mounted () {
    this.ruleForm.pid = this.pid
    this.ruleForm = this.rowData
    console.log(this.ruleForm, this.pid)
  },
  watch: {
    pid () {
      this.ruleForm.pid = this.pid
      console.log(this.ruleForm, this.pid)
    },
    rowData () {
      this.ruleForm = this.rowData
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.idCategory = this.idCategory
          this.saveLoading = true
          const request = this.type === 'add' ? addTermList(this.ruleForm) : editTermList(this.ruleForm)

          request.then(() => {
            this.$message.success(this.type === 'add' ? '新增成功！' : '修改成功！')
            this.saveLoading = false
            this.$emit('handleSuccess')
          }).catch(() => {
            this.saveLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel () {
      this.$emit('handleCancel')
    }
  }
}
</script>

<style lang="scss" scoped>
  .add {
    padding-right: 40px;

    .width-all {
      width: 100%;

      ::v-deep input {
        text-align: left;
      }
    }
  }
</style>
